from typing import Literal
class AgenticRAG:
def __init__(self, retriever):
self.retriever = retriever
def analyze_query(self, question: str) -> dict:
"""LLM analyzes query and decides retrieval strategy."""
analysis_prompt = f"""
Analyze this query and determine the best retrieval strategy:
Query: {question}
Respond in JSON:
{{
"query_type": "factual | conceptual | multi_hop | temporal",
"key_entities": ["entity1", "entity2"],
"time_range": "optional ISO date range",
"search_strategy": "keyword | semantic | hybrid",
"metadata_filters": {{"document_type": "value"}},
"reasoning": "brief explanation"
}}
"""
response = openai.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": analysis_prompt}],
response_format={"type": "json_object"}
)
import json
return json.loads(response.choices[0].message.content)
def construct_query(self, question: str, analysis: dict) -> tuple:
"""Build optimized query based on analysis."""
# Extract key terms for searching
search_query = " ".join(analysis['key_entities'])
# Build metadata filters
filters = analysis.get('metadata_filters', {})
# Add temporal filter if specified
if analysis.get('time_range'):
filters['created_at'] = {'$gte': analysis['time_range']}
return search_query, filters
def query(self, question: str) -> str:
"""Agentic RAG: LLM-guided retrieval."""
# Step 1: Analyze query
analysis = self.analyze_query(question)
print(f"Analysis: {analysis['reasoning']}")
# Step 2: Construct optimized query
search_query, filters = self.construct_query(question, analysis)
# Step 3: Retrieve with constructed query + filters
results = self.retriever.search(
search_query,
metadata_filters=filters,
top_k=5
)
# Step 4: Generate answer
context = "\n\n".join([r['document'] for r in results])
response = openai.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": f"Context:\n{context}\n\nQuestion: {question}"
}]
)
return response.choices[0].message.content
# Example
agentic = AgenticRAG(retriever)
# Query 1: Factual
answer = agentic.query("What is our company's PTO policy?")
# Analysis might select: keyword search, filter to HR docs
# Query 2: Temporal
answer = agentic.query("What changed in our benefits since last year?")
# Analysis might select: semantic search, filter to docs after 2024-01-01
# Query 3: Multi-hop
answer = agentic.query("Which engineers on the data team have Python experience?")
# Analysis might select: entity search for ["data team", "Python"], multi-hop